//
//  WalletMainVC.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/5/27.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "WalletMainVC.h"

@interface WalletMainVC () {
    double yesterdayPoint;
    double totalPoint;
    NSString *address;
}

@property(weak, nonatomic)IBOutlet UIButton   *backBt;
@property(weak, nonatomic)IBOutlet UIButton   *settingBt;

@property(weak, nonatomic)IBOutlet UIView   *bindView;
@property(weak, nonatomic)IBOutlet UILabel  *bProfitLb;
@property(weak, nonatomic)IBOutlet UIButton *bDetailBt;
@property(weak, nonatomic)IBOutlet UIButton *bModifyAddrBt;
@property(weak, nonatomic)IBOutlet UILabel  *bAccountLb;

@property(weak, nonatomic)IBOutlet UIView   *unbindView;
@property(weak, nonatomic)IBOutlet UILabel  *uProfitLb;
@property(weak, nonatomic)IBOutlet UIButton *uDetailBt;
@property(weak, nonatomic)IBOutlet UILabel  *uTotalProfitLb;
@property(weak, nonatomic)IBOutlet UIButton *uTotalProfitBt;
@property(weak, nonatomic)IBOutlet UIButton *extractBt;
@property(weak, nonatomic)IBOutlet UIButton *bindBt;

@end

@implementation WalletMainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initData];
    [self initView];
    [self updateView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestData];
    [self requestPoint];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initData {
    yesterdayPoint = getValueByKey(@"ss_yesterday_point") ? [getValueByKey(@"ss_yesterday_point") doubleValue] : 0;
    totalPoint = getValueByKey(@"ss_point") ? [getValueByKey(@"ss_point") doubleValue] : 0;
    address = getValueByKey(@"ss_address");
}

- (void)initView {
    self.bDetailBt.layer.cornerRadius = globalRadius;
    self.bModifyAddrBt.layer.cornerRadius = globalRadius;
    self.uDetailBt.layer.cornerRadius = globalRadius;
    self.uTotalProfitBt.layer.cornerRadius = globalRadius;
    self.extractBt.layer.cornerRadius = globalRadius;
    self.bindBt.layer.cornerRadius = globalRadius;
    [[self.backBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [[self.settingBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"WalletSettingVCID"];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [[self.bDetailBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"WalletYesterdayProfitVCID"];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [[self.bModifyAddrBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"WalletBindVCID"];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [[self.uDetailBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"WalletYesterdayProfitVCID"];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [[self.uTotalProfitBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [JPToast showText:@"敬请期待"];
    }];
    [[self.extractBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"WalletExtractVCID"];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [[self.bindBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"WalletBindVCID"];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [self.bindView setHidden:YES];
    [self.unbindView setHidden:YES];
}

- (void)updateView {
    self.bProfitLb.text = [NSString stringWithFormat:@"%f", self->yesterdayPoint];
    self.uProfitLb.text = [NSString stringWithFormat:@"%f", self->yesterdayPoint];
    self.uTotalProfitLb.text = [NSString stringWithFormat:@"%f", self->totalPoint];
    if (address) {
        self.bAccountLb.text = address;
        [self.bindView setHidden:NO];
        [self.unbindView setHidden:YES];
    } else {
        [self.bindView setHidden:YES];
        [self.unbindView setHidden:NO];
    }
}

- (void)requestData {
    [self showLoading:@"加载中"];
    [[[HttpRequest shareInstance] signGet:s_wallet map:nil] subscribeNext:^(id x) {
        [self hideLoading];
        if ([x[@"code"] intValue] == 200) {
            self->address = x[@"data"];
            saveValueByKey(self->address, @"ss_address");
            [self updateView];
        } else {
            if ([x[@"code"] intValue] == 1005) {
                [self updateView];
            }
            [JPToast showText:x[@"message"]];
        }
    } error:^(NSError *error) {
        [self hideLoading];
        [JPToast showText:@"网络错误"];
    }];
}

- (void)requestPoint {
    [[[HttpRequest shareInstance] signGet:w_point map:nil] subscribeNext:^(id x) {
        if ([x[@"code"] intValue] == 200) {
            @try {
                NSDictionary *data = string2Json(x[@"data"]);
                self->totalPoint = [[data objectForKey:@"point"] doubleValue];
                saveValueByKey([NSNumber numberWithDouble:self->totalPoint], @"ss_point");
                [self updateView];
            } @catch (NSException *exception) {
                self->totalPoint = 0;
            }
        } else {
            [JPToast showText:x[@"message"]];
        }
    } error:^(NSError *error) {
        [JPToast showText:@"网络错误"];
    }];
    
    NSString *url2 = [NSString stringWithFormat:@"%@/yesterday_point", w_yesterday_point_device];
    [[[HttpRequest shareInstance] signGet:url2 map:nil] subscribeNext:^(id x) {
        if ([x[@"code"] intValue] == 200) {
            id data = x[@"data"];
            self->yesterdayPoint = data ? [data doubleValue] : 0;
            saveValueByKey([NSNumber numberWithDouble:self->yesterdayPoint], @"ss_yesterday_point");
            [self updateView];
        } else {
            [JPToast showText:@"网络错误"];
        }
    } error:^(NSError *error) {
        [JPToast showText:@"网络错误"];
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end
